package com.example.trafficexam2.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.trafficexam2.R;
import com.example.trafficexam2.bean.Sense;
import com.example.trafficexam2.dao.SenseDao;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by Administrator on 2018/11/22/022.
 */

public class EnvironmentFragment extends Fragment implements View.OnClickListener{
    View view;
    private TextView temperaturetv;
    private TextView humiditytv;
    private TextView illuminationtv;
    private TextView co2tv;
    private TextView pm25tv;
    private TextView roadtv;
    private Timer timer;
    private TimerTask task;
    private LinearLayout linearLayout1;
    private LinearLayout linearLayout2;
    private LinearLayout linearLayout3;
    private LinearLayout linearLayout4;
    private LinearLayout linearLayout5;
    private LinearLayout linearLayout6;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.environment_fragment, container, false);
        initView();
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                initData();
            }
        };
        timer.schedule(task,0,3000);
        return view;
    }

    private void initView() {
        linearLayout1 = view.findViewById(R.id.linear1);
        linearLayout2 = view.findViewById(R.id.linear2);
        linearLayout3 = view.findViewById(R.id.linear3);
        linearLayout4 = view.findViewById(R.id.linear4);
        linearLayout5 = view.findViewById(R.id.linear5);
        linearLayout6 = view.findViewById(R.id.linear6);
        linearLayout1.setOnClickListener(this);
        linearLayout2.setOnClickListener(this);
        linearLayout3.setOnClickListener(this);
        linearLayout4.setOnClickListener(this);
        linearLayout5.setOnClickListener(this);
        linearLayout6.setOnClickListener(this);
        temperaturetv = view.findViewById(R.id.temperature_tv);
        humiditytv = view.findViewById(R.id.humidity_tv);
        illuminationtv = view.findViewById(R.id.illumination_tv);
        co2tv = view.findViewById(R.id.co2_tv);
        pm25tv = view.findViewById(R.id.pm25_tv);
        roadtv = view.findViewById(R.id.road_tv);
    }

    private void initData() {
        String url = "http://47.106.153.72:8081/transportservice/action/GetAllSense.do";
        String url1 = "http://47.106.153.72:8081/transportservice/action/GetRoadStatus.do";
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("UserName","user1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        final JSONObject jsonObject1 = new JSONObject();
        try {
            jsonObject1.put("RoadId",1);
            jsonObject1.put("UserName","user1");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestQueue requestQueue = Volley.newRequestQueue(getContext());
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                int pm25 = jsonObject.optInt("pm2.5");
                Sense sense = new Sense(pm25,jsonObject.optInt("co2"),jsonObject.optInt("LightIntensity"),jsonObject.optInt("humidity"),jsonObject.optInt("temperature"));
                new SenseDao(getContext()).insert(sense);
                co2tv.setText(jsonObject.optString("co2"));
                if (jsonObject.optInt("co2")>=500){
                    linearLayout4.setBackgroundColor(Color.RED);
                }else {
                    linearLayout4.setBackgroundColor(Color.GREEN);
                }
                pm25tv.setText(jsonObject.optString("pm2.5"));
                if (jsonObject.optInt("pm2.5")>=240){
                    linearLayout5.setBackgroundColor(Color.RED);
                }else {
                    linearLayout5.setBackgroundColor(Color.GREEN);
                }
                temperaturetv.setText(jsonObject.optString("temperature"));
                if (jsonObject.optInt("temperature")>=30){
                    linearLayout1.setBackgroundColor(Color.RED);
                }else {
                    linearLayout1.setBackgroundColor(Color.GREEN);
                }
                humiditytv.setText(jsonObject.optString("humidity"));
                if (jsonObject.optInt("humidity")>=600){
                    linearLayout3.setBackgroundColor(Color.RED);
                }else {
                    linearLayout3.setBackgroundColor(Color.GREEN);
                }
                illuminationtv.setText(jsonObject.optString("LightIntensity"));
                if (jsonObject.optInt("LightIntensity")>=70){
                    linearLayout2.setBackgroundColor(Color.RED);
                }else {
                    linearLayout2.setBackgroundColor(Color.GREEN);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.POST, url1, jsonObject1, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                roadtv.setText(jsonObject.optString("Status"));
                if (jsonObject.optInt("Status")>=3){
                    linearLayout6.setBackgroundColor(Color.RED);
                }else {
                    linearLayout6.setBackgroundColor(Color.GREEN);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getContext(), "网络错误", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(jsonObjectRequest);
        requestQueue.add(jsonObjectRequest1);
    }
    @Override
    public void onDestroyView() {
        task.cancel();
        super.onDestroyView();
    }

    @Override
    public void onClick(View view) {
        task.cancel();
        switch (view.getId()){
            case R.id.linear1:
                Intent intent = new Intent(getContext(),ViewFragement.class);
                startActivity(intent);
                break;
            case R.id.linear2:
                Intent intent1 = new Intent(getContext(),ViewFragement.class);
                startActivity(intent1);
                break;
            case R.id.linear3:
                Intent intent2 = new Intent(getContext(),ViewFragement.class);
                startActivity(intent2);
                break;
            case R.id.linear4:
                Intent intent3 = new Intent(getContext(),ViewFragement.class);
                startActivity(intent3);
                break;
            case R.id.linear5:
                Intent intent4 = new Intent(getContext(),ViewFragement.class);
                startActivity(intent4);
                break;
            case R.id.linear6:
                Intent intent5 = new Intent(getContext(),ViewFragement.class);
                startActivity(intent5);
                break;
        }
    }
}
